package com.frt.sygang.controller;/**
 * Created by 冯瑞涛 on 2022/9/22 10:35
 */

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.frt.sygang.common.R;
import com.frt.sygang.entity.ShoppingCart;
import com.frt.sygang.sevice.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 冯瑞涛
 * @version 1.0
 * @date 2022/9/22 10:35
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车记录方法
     *
     * @param shoppingCart
     * @param request
     * @return
     */
    @PostMapping("/add")
    public R<String> insShoppingCart(@RequestBody ShoppingCart shoppingCart, HttpServletRequest request) {
        log.info("购物车的到--->" + shoppingCart.toString());
        Long userId = (Long) request.getSession().getAttribute("user");
        shoppingCart.setUserId(userId);
        String dishFlavor = shoppingCart.getDishFlavor();
        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, userId);
        queryWrapper.eq(setmealId!=null,ShoppingCart::getSetmealId,setmealId);
        queryWrapper.eq(dishId!=null,ShoppingCart::getDishId, dishId);
        queryWrapper.eq(dishFlavor!=null,ShoppingCart::getDishFlavor, dishFlavor);
        ShoppingCart byId = shoppingCartService.getOne(queryWrapper);
        log.info("获取到的购物车信息："+byId);
        boolean b;
        if (byId != null) {
            Long id = byId.getId();
            Integer number1 = byId.getNumber();
            Integer number = number1 + 1;
            LambdaUpdateWrapper<ShoppingCart> wrapper = new LambdaUpdateWrapper<>();
            wrapper.set(ShoppingCart::getNumber, number);
            wrapper.eq(ShoppingCart::getId, id);
            b = shoppingCartService.update(wrapper);
        } else {
            b = shoppingCartService.save(shoppingCart);
        }
        if (b) {
            return R.success("成功");
        }
        return R.error("失败");
    }

    /**
     * 根据用户id查询购物车信息
     * @param request
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> getShopping(HttpServletRequest request) {
        Long userId = (Long) request.getSession().getAttribute("user");
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, userId);
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);
    }

    /**
     *
     * @param shoppingCart
     * @param request
     * @return
     */
    @PostMapping("/sub")
    public R<String> subShopping(@RequestBody ShoppingCart shoppingCart, HttpServletRequest request) {
        shoppingCartService.subShopping(shoppingCart.getDishId(),shoppingCart.getSetmealId(),request);
        return R.success("修改成功");
    }

    @DeleteMapping("/clean")
    public R<String> cleanShopping(HttpServletRequest request){
        Long userId = (Long) request.getSession().getAttribute("user");
        LambdaUpdateWrapper<ShoppingCart> wrapper=new LambdaUpdateWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,userId);
        shoppingCartService.remove(wrapper);
        return R.success("购物车已清空");
    }

}
